home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C04 / CLibTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.2 KB  |  44 lines

  1. //: C04:CLibTest.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} CLib
  7. // Test the C-like library
  8. #include "CLib.h"
  9. #include <fstream>
  10. #include <iostream>
  11. #include <string>
  12. #include <cassert>
  13. using namespace std;
  14.  
  15. int main() {
  16.   // Define variables at the beginning
  17.   // of the block, as in C:
  18.   CStash intStash, stringStash;
  19.   int i;
  20.   char* cp;
  21.   ifstream in;
  22.   string line;
  23.   const int bufsize = 80;
  24.   // Now remember to initialize the variables:
  25.   initialize(&intStash, sizeof(int));
  26.   for(i = 0; i < 100; i++)
  27.     add(&intStash, &i);
  28.   for(i = 0; i < count(&intStash); i++)
  29.     cout << "fetch(&intStash, " << i << ") = "
  30.          << *(int*)fetch(&intStash, i)
  31.          << endl;
  32.   // Holds 80-character strings:
  33.   initialize(&stringStash, sizeof(char)*bufsize);
  34.   in.open("CLibTest.cpp");
  35.   assert(in);
  36.   while(getline(in, line))
  37.     add(&stringStash, line.c_str());
  38.   while((cp = (char*)fetch(&stringStash,i++))!=0)
  39.     cout << "fetch(&stringStash, " << i << ") = "
  40.          << cp << endl;
  41.   cleanup(&intStash);
  42.   cleanup(&stringStash);
  43. } ///:~
  44.